/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs575.coresystem;

import cs575.dbentities.GroupEx;
import cs575.dbentities.GroupMember;
import cs575.dbentities.Role;
import cs575.dbentities.Task;
import cs575.dbentities.TaskAssign;
import cs575.dbentities.TaskUser;
import cs575.dbentities.User;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Column;

/**
 * Create a static array of all roles and permissions during facade intiliatization
 * 
 * @author bms
 */
public class Authorization {
    public static final String G_invite_user = "invite_user";
    public static final String G_remove_user = "remove_user";
    public static final String G_read_Group = "read_Group";
    public static final String G_edit_group = "edit_group";
    public static final String G_delete_group = "delete_group";
    public static final String G_set_member_role = "set_member_role";
    public static final String G_add_task_to_group = "add_task_to_group";
    public static final String G_remove_task_from_group = "remove_task_from_group";
    private List<String> group_columns;
    
    public static final String T_read = "read";
    public static final String T_edit = "edit";
    public static final String T_delete = "delete";
    public static final String T_set_permission = "set_permission";
    public static final String T_complete = "complete";
    private List<String> task_columns;
    
    private HashMap<Integer,HashMap<String,Boolean>> group_permissions;
    private HashMap<Integer,HashMap<String,Boolean>> task_permissions;
    private User user;
    
    public Authorization() {
    }
    
    public Authorization(User user) {
        this.user = user;
    }
    
    public void loadData()
    {
        loadGroupPermissions();
        loadTaskPermissions();
    }

    private void loadGroupPermissions() {
        int numOfGroups = user.getGroupMemberList().size();
        group_permissions = new HashMap<Integer,HashMap<String,Boolean>>(numOfGroups);
        
//        group_columns = new ArrayList<String>();
//        for (Field field : user.getClass().getDeclaredFields()) {
//            Column column = field.getAnnotation(Column.class);
//            if (column != null) {
//                group_columns.add(column.name());
//            }
//        }
        
        for (GroupMember member : user.getGroupMemberList()) {
            Role role = member.getRole();
            HashMap<String, Boolean> permissions = new HashMap<String, Boolean>(7);
            permissions.put(G_invite_user, role.getInviteUser());
            permissions.put(G_remove_user, role.getRemoveUser());
            permissions.put(G_remove_task_from_group, role.getRemoveTaskFromGroup());
            permissions.put(G_set_member_role, role.getSetMemberRole());
            permissions.put(G_add_task_to_group, role.getAddTaskToGroup());
            permissions.put(G_edit_group, role.getEditGroup());
            permissions.put(G_delete_group, role.getDeleteGroup());
            permissions.put(G_read_Group, role.getReadGroup());
            group_permissions.put(member.getGroupMemberPK().getGroupId(), permissions);
        }
    }

    private void loadTaskPermissions() {
        int numOfTasks = user.getTaskAssignedList().size();
        task_permissions = new HashMap<Integer,HashMap<String,Boolean>>(numOfTasks);
        
//        group_columns = new ArrayList<String>();
//        for (Field field : user.getClass().getDeclaredFields()) {
//            Column column = field.getAnnotation(Column.class);
//            if (column != null) {
//                group_columns.add(column.name());
//            }
//        }        
        for (TaskUser task : user.getTaskUserList()) {
            HashMap<String, Boolean> permissions = new HashMap<String, Boolean>(7);
            permissions.put(T_read, task.getRead());
            permissions.put(T_edit, task.getEdit());
            permissions.put(T_delete, task.getDelete());
            permissions.put(G_set_member_role, task.getSetPermission());
            permissions.put(T_set_permission,task.getSetPermission());
            permissions.put(T_complete, task.getComplete());
            task_permissions.put(task.getTaskUserPK().getTaskId(), permissions);
        }
       
    }
    
    public boolean isAuthorized(GroupEx group,String action)
    {
        boolean op = false;
        HashMap<String, Boolean> permissions = group_permissions.get(group.getGroupId());
        if(permissions != null){
            op = permissions.get(action);
        }
        return op;
    }
    
    public boolean isAuthorized(Task task,String action)
    {
        boolean op = false;
        HashMap<String, Boolean> permissions = task_permissions.get(task.getTaskId());
        if(permissions != null){
            op = permissions.get(action);
        }
        return op;
    }
    
}
